#include "tfilesave.h"

TFileSave::TFileSave(QObject *parent)
    : QObject{parent}
{
    thread = new QThread(this);
    dateTime = new QDateTime;
    this->moveToThread(thread);
    thread->start(QThread::IdlePriority);
}

void TFileSave::on_saveFile(QByteArray ar, QString fileName)
{
    QFile file;
    QString extractTo = QCoreApplication::applicationDirPath();
    QString dirName = QDir::cleanPath(extractTo+QDir::separator()+".."+QDir::separator()+"log"+QDir::separator()+dateTime->currentDateTime().toString("yyyyMMdd"));
    QDir dir(dirName);
    if (!dir.exists())
    {
        if(!dir.mkpath(dirName))
        {
            qDebug()<<"日志目录创建失败";
            return;
        }
    }
    file.setFileName(dirName+QDir::separator()+fileName+dateTime->currentDateTime().toString("yyyyMMddhhmmss")+".txt");
    if(!file.open(QIODevice::WriteOnly))
    {
        qDebug()<<"文件打开失败";
        file.close();
        return;
    }
    file.write(ar);
    file.close();
}
